﻿package com.victim.snake.snake {
	import flash.geom.Rectangle;
	import flash.media.Video;
	import com.victim.snake.user.User;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import com.victim.snake.util.Loader;
	import flash.events.EventDispatcher;

	//import ascb.util.NumberUtilities;
	
	//食物
	public class Food extends EventDispatcher{
		
		static public var Type1Btm:Bitmap=null;	//增长
		static public var Type2Btm:Bitmap=null;	//速度
		static private var Type1BtmState:uint=0;	//0末下载,1下载中,2以下载
		static private var Type2BtmState:uint=0;	//0末下载,1下载中,2以下载
		
		
		private var id:int;
		private var rect:Rectangle=new Rectangle(0,0,20,20);	//食物方块
		private var type:uint;	//食物类型( 1增长 2速度
		
		//构造函数
		public function Food(id:int,type:uint,x:Number,y:Number,width:Number,height:Number) {
			this.setId(id);
			this.setX(x);
			this.setY(y);
			this.setWidth(width);
			this.setHeight(height);
			this.setType(type);
		}
		
		//随机坐标
		/*public function random():void{
			this.setX(NumberUtilities.random(0,980,20));
			this.setY(NumberUtilities.random(0,580,20));
		}
		*/
		
		
		//开始下载Type1Btm
		private function loadType1Btm():void{
			if(Food.Type1BtmState==0 || Food.Type1BtmState==1){
				Food.Type1BtmState=1;
				new Loader("image/food/type1Btm.png",loadType1BtmEvent);
			}else{
				var foodEvent:FoodEvent=new FoodEvent(FoodEvent.Type1BtmLoad);
				this.dispatchEvent(foodEvent);
			}
		}		
		//当Type1Btm图片下载完成时调用的方法
		private function loadType1BtmEvent(e:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(e.target);
			Food.Type1Btm=Bitmap(loaderInfo.content);
			var foodEvent:FoodEvent=new FoodEvent(FoodEvent.Type1BtmLoad);
				this.dispatchEvent(foodEvent);
			Food.Type1BtmState=2;
		}
		
		//开始下载Type2Btm
		private function loadType2Btm():void{
			if(Food.Type2BtmState==0 || Food.Type2BtmState==1){
				Food.Type2BtmState=1;
				new Loader("image/food/type2Btm.png",loadType2BtmEvent);
			}else{
				var foodEvent:FoodEvent=new FoodEvent(FoodEvent.Type2BtmLoad);
				this.dispatchEvent(foodEvent);
			}
		}		
		//当Type2Btm图片下载完成时调用的方法
		private function loadType2BtmEvent(e:Event):void{			
			var loaderInfo:LoaderInfo=LoaderInfo(e.target);
			Food.Type2Btm=Bitmap(loaderInfo.content);
			var foodEvent:FoodEvent=new FoodEvent(FoodEvent.Type2BtmLoad);
				this.dispatchEvent(foodEvent);
			Food.Type2BtmState=2;
		}
		
		public function loadBtm():void{
			this.loadType1Btm();
			this.loadType2Btm();
		}
		
		
		public function getBitmap():Bitmap{
			var bitmap:Bitmap=null;
			switch(this.type){
				case 1:
					 bitmap=new Bitmap(Food.Type1Btm.bitmapData);
					break;
				case 2:
					bitmap=new Bitmap(Food.Type2Btm.bitmapData);
					break;
				default:
					bitmap=new Bitmap(Food.Type1Btm.bitmapData);
					break;				
			}
			bitmap.x=this.getX();
			bitmap.y=this.getY();
			return bitmap;
		}
		
		//get and set
		public function getType():uint{
			return this.type;
		}
		
		public function setType(type:uint):void
		{
			this.type = type;
		}
		
		public function getX():Number
		{
			return this.rect.x;
		}

		public function setX(x:Number)
		{
			this.rect.x = x;
		}

		public function getY():Number
		{
			return this.rect.y;
		}

		public function setY(y:Number)
		{
			this.rect.y = y;
		}

		public function getWidth():Number
		{
			return this.rect.width;
		}

		public function setWidth(width:Number)
		{
			this.rect.width = width;
		}

		public function getHeight():Number
		{
			return this.rect.height;
		}

		public function setHeight(height:Number)
		{
			this.rect.height = height;
		}
		
		public function getId():int
		{
			return this.id;
		}

		public function setId(id:int):void
		{
			this.id=id;
		}

	}
	
}
